package com.wechat.servlet;

import com.alibaba.fastjson.JSONArray;
import com.wechat.DAO.DaoImpl.MessageDAOIpml;
import com.wechat.DAO.MessageDAO;
import com.wechat.servlet.Utils.Base64Decoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Array;

//注意！！post只能发送端发，get只能接收端发
public class ClearMsgServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        long M_Time = Long.valueOf(request.getParameter("M_Time"));
        MessageDAO messageDAO = new MessageDAOIpml();
        if(messageDAO.sendOk(M_Time)) {
            System.out.println("clearMsg刚刚改了一条消息");
            response.getWriter().write("success");
        }else {
            response.getWriter().write("fail");
        }
    }

    //9.根据查询内容修改对应消息的IF_Receive;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("有人接收到消息");
        String data = request.getParameter("data");
        MessageDAO messageDAO = new MessageDAOIpml();
        if(data!=null){
            String json = Base64Decoder.decode(data);
            System.out.println(json);
            JSONArray jsonArray = JSONArray.parseArray(json);
            System.out.println(jsonArray);
            for (int i = 0;i < jsonArray.size(); i++){
                long M_Time = jsonArray.getLong(i);
            if(!messageDAO.receiveOk(M_Time)) {
                response.getWriter().write("fail");
                return;
            }
            }
            response.getWriter().write("success");
        }else {
            System.out.println("ReceiveOK失败——没有接收到消息M_Time");
            response.getWriter().write("fail");
        }
    }
}
